// Copyright 2009 Pigasus.
// GNU GPL v2.
// Author: John Zhang and Jordan Rupprecht.

#include "push.h"

#include <string>

#include "common.h"
#include "jobthread.h"
#include "pigasus-types.h"
#include "threadpool.h"

using namespace std;

extern ThreadPool *job_thread_pool;
extern pthread_mutex_t *mutex_job_thread_pool;

extern Servers *global_servers;

int Push(string job_name, List common, List inputs, List *outputs) {
  string job_executable = job_name + ".out";
  List common_with_job = NewList(File(job_executable)) + common;

  // debug("Push: %d, %d\n", common_with_job.GetSize(), inputs.GetSize());
  for (int i = 0; i < common.GetSize(); i++) {
    debug("--Push: %s\n", ((string*)common[i])->c_str());
  }
  
  // Add job_executable to common.
  Thread *j = new JobThread(*global_servers, common_with_job, inputs, outputs);
  // Add job!
  pthread_mutex_lock(mutex_job_thread_pool);
  int job_id = job_thread_pool->AddThread(j);
  pthread_mutex_unlock(mutex_job_thread_pool);  

  return job_id;
}

